/**
 * @(#)TaskInstanceOpinionTag.java Nov 30, 2009
 *   
 * Copyright  (c) 2009-2010 Wilmar IT Service,  Inc.   
 * All  rights  reserved.   
 * TODO
 */

package com.wilmar.process.web.taglibs;

import javax.servlet.jsp.JspWriter;

import com.wilmar.process.core.ProcessSystemDeclareKeys;
import com.wilmar.process.enumerate.EnumProcessTagSupportPublicAttribute;
import com.wilmar.process.enumerate.EnumSystemLanguageCountryMap;

/**
 * @author MaKuBEX Email:makubexnet@gmail.com
 * @version 1.0.0
 */
public class TaskInstanceOpinionTag extends ProcessTagSupport {

	/**
	 * @serialField
	 */
	private static final long serialVersionUID = -647169927602818062L;

	private Boolean require;
	private String mirror;
	private String cols;
	private String rows;
	private String defaultvalue;
	private Integer minlength;
	private Integer maxlength;

	private String type;

	/**
	 * @see org.springframework.web.servlet.tags.RequestContextAwareTag#doStartTagInternal()
	 */
	protected int doStartTagInternal() throws Exception {
		StringBuffer html = new StringBuffer();
		html.append("<textarea");
		html.append(" name=\"processInformation.opinion\"");
		super.appendPublicAttribute(html,
				EnumProcessTagSupportPublicAttribute.ID);
		super.appendPublicAttribute(html,
				EnumProcessTagSupportPublicAttribute.CSSCLASS);
		super.appendAttribute(html, "cols", this.cols, null);
		super.appendAttribute(html, "rows", this.rows, null);
		html.append(">");
		if (this.defaultvalue != null && this.defaultvalue.trim().length() != 0) {
			html.append(this.defaultvalue);
		}
		html.append("</textarea>\n");
		html.append("<script type=\"text/javascript\">\n");
		html.append("PROCESS_FORM_OPINION_ID=");
		html.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
		html.append(super.id);
		html.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
		html.append(";\n");
		if (this.mirror != null && this.mirror.trim().length() != 0) {
			html.append("PROCESS_FORM_OPINION_MIRROR_ID=");
			html.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			html.append(this.mirror);
			html.append(ProcessSystemDeclareKeys.SINGLE_QUOTES);
			html.append(";\n");
		}
		if (this.require != null && this.require) {
			html.append("TASKINSTANCE_OPINION_EMPTY_CHECK=true;\n");
		}
		if (this.minlength != null && this.minlength > 0) {
			html.append("TASKINSTANCE_OPINION_MINLENGTH=");
			html.append(this.minlength);
			html.append(";\n");
		}
		if (this.maxlength != null && this.maxlength > 0) {
			html.append("TASKINSTANCE_OPINION_MAXLENGTH=");
			html.append(this.maxlength);
			html.append(";\n");
		}
		if (this.type != null && this.type.trim().equalsIgnoreCase("ckeditor")) {
			html.append("PROCESS_FORM_OPINION_TYPE='ckeditor';\n");
			html.append("$(function() { \n");
			html.append("OPINION_CKEDITOR_INSTANCE_LANGUAGE = '"
					+ super.getUserLocale().getLanguage().toLowerCase());
			String suffix = "";
			try {
				EnumSystemLanguageCountryMap.valueOf(super.getUserLocale()
						.getCountry().toUpperCase());
				suffix = ProcessSystemDeclareKeys.MINUS
						+ super.getUserLocale().getCountry().toLowerCase();
			} catch (Exception e) {
			}
			html.append(suffix);
			html.append("';\n");
			html.append("$('#" + this.id + "').ckeditor({ \n");
			html.append("toolbar : OPINION_CKEDITOR_INSTANCE_TOOLBAR,\n");
			html.append("language : OPINION_CKEDITOR_INSTANCE_LANGUAGE\n");
			html.append("});\n");
			html.append("OPINION_CKEDITOR_INSTANCE=CKEDITOR.instances."
					+ this.id + ";\n");
			html.append("});\n");
		}
		html.append("</script>\n");
		JspWriter outer = super.getOut();
		outer.write(html.toString());
		return SKIP_BODY;
	}

	/**
	 * @return the mirror
	 */
	public String getMirror() {
		return mirror;
	}

	/**
	 * @param mirror
	 *            the mirror to set
	 */
	public void setMirror(String mirror) {
		this.mirror = mirror;
	}

	/**
	 * @return the cols
	 */
	public String getCols() {
		return cols;
	}

	/**
	 * @param cols
	 *            the cols to set
	 */
	public void setCols(String cols) {
		this.cols = cols;
	}

	/**
	 * @return the rows
	 */
	public String getRows() {
		return rows;
	}

	/**
	 * @param rows
	 *            the rows to set
	 */
	public void setRows(String rows) {
		this.rows = rows;
	}

	/**
	 * @return the defaultvalue
	 */
	public String getDefaultvalue() {
		return defaultvalue;
	}

	/**
	 * @param defaultvalue
	 *            the defaultvalue to set
	 */
	public void setDefaultvalue(String defaultvalue) {
		this.defaultvalue = defaultvalue;
	}

	/**
	 * @return the require
	 */
	public Boolean getRequire() {
		return require;
	}

	/**
	 * @param require
	 *            the require to set
	 */
	public void setRequire(Boolean require) {
		this.require = require;
	}

	/**
	 * @return the minlength
	 */
	public Integer getMinlength() {
		return minlength;
	}

	/**
	 * @param minlength
	 *            the minlength to set
	 */
	public void setMinlength(Integer minlength) {
		this.minlength = minlength;
	}

	/**
	 * @return the maxlength
	 */
	public Integer getMaxlength() {
		return maxlength;
	}

	/**
	 * @param maxlength
	 *            the maxlength to set
	 */
	public void setMaxlength(Integer maxlength) {
		this.maxlength = maxlength;
	}

	/**
	 * @return the type
	 */
	public String getType() {
		return type;
	}

	/**
	 * @param type
	 *            the type to set
	 */
	public void setType(String type) {
		this.type = type;
	}

}
